package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor
public class LessonChangeListener {

    final ILearningLessonService learningLessonService;

    //监听mq的消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "learning.lesson.pay.queue",durable = "true"),
            exchange = @Exchange(name = "MqConstants.Exchange.ORDER_EXCHANGE",type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_PAY_KEY
    ))
    public void onMsg(OrderBasicDTO order){
        //先判断数据是否不为空
        if (order==null||order.getUserId()==null|| CollUtils.isEmpty(order.getCourseIds())){
            //不要抛出异常，否则会消费者重试，浪费资源
            log.info("收到的mq消息为空");
            return;
        }
        log.info("收到的mq消息如下:{}",order);
        learningLessonService.addUserLessons(order.getUserId(),order.getCourseIds());
    }
}
